%REM
	Agent lsOpenServerCommand
	Created Apr 7, 2010 by admin/org
	Description: Comments for Agent
%END REM
Option Public
Option Declare

Class DirList
	Public level As Integer
	Public prefix As String
	Public entries List As String
End Class
Sub Initialize	
'	
' Certain system databases are hidden by OpenServer.
' We list these databases here. You can modify
' this list if you want.
	Dim hidden List As String
	hidden("admin4.nsf") = ""
	hidden("busytime.nsf") = ""
	hidden("setup.nsf") = ""
'
' Create a list of databases on the server
'
	Dim pathmap List As DirList
	Dim session as New NotesSession
	Dim dbdir As NotesDbDirectory
	Dim db As NotesDatabase
	Dim filePath As String
	Dim path As String
	Dim subdir As DirList
	Dim segment As Integer
	Dim server As NotesName
	Dim lowest As string
	
	Set dbdir = session.GetDbDirectory("")
	Set db = dbdir.GetFirstDatabase(DATABASE)
	While Not (db Is Nothing)
		filePath = db.FilePath
' Check if this database is supposed to be hidden
		If (IsElement(hidden(filePath))) Then GoTo NextDb
' Change Windows backslashes to forward slashes
		While (InStr(filePath,"\") > 0)
			Mid(filePath,InStr(filePath,"\")) = "/"
		Wend
' Get actual subdirectory path (everything before the file name)
		path = StrLeftBack(filePath,"/")
' Normalize the path to start and end with a slash
		If path = "" Then path = "/" Else path = "/" & path & "/"
		If Not IsElement(pathmap(path)) Then
' Add this path to the directory list
			Set pathmap(path) = New DirList
			Set subdir = pathmap(path)
			subdir.level = 0
			subdir.prefix = ""
' Determine how many levels deep this path is
			segment = InStr(path,"/")
			While (segment > 0)
				subdir.level = subdir.level + 1
				If subdir.level > 1 Then subdir.prefix = subdir.prefix & ". . "
				segment = InStr(segment + 1,path,"/")
			Wend
		End If
		Set subdir = pathmap(path)
' Save the name to be displayed for this database
		If (db.Title <> "") Then
			subdir.entries(db.FileName) = db.Title
		Else
			subdir.entries(db.FileName) = "(No title: " & db.FileName & ")"
		End If
NextDb:
		Set db = dbdir.GetNextDatabase
	Wend
	
'
' Generate HTML, looping over directories in sort order
'
	Set server = New NotesName(session.CurrentDatabase.Server)
	Print "<h2>"; server.Abbreviated; "</h2><ul>"
	Do
' Search the directory list for the lowest-sorting path
		lowest = ""
		ForAll p In pathmap
			If lowest = "" Then
				lowest = ListTag(p)
			ElseIf ListTag(p) < lowest Then
				lowest = ListTag(p)
			End If
		End ForAll
		If lowest = "" Then Exit Do ' no entries left in list, so we're done
		Set subdir = pathmap(lowest)
		If subdir.level > 1 Then
' Generate HTML for subdirectory name
			Print Mid(subdir.prefix,4);
			Print {<img src="/icons/afolder.gif" border=0>};
			Print StrRight(lowest,"/",5,subdir.level - 1);{<br>}
		End If
' Generate HTML for all entries in the database list for this path
		ForAll e In subdir.entries
			Print subdir.prefix;
			Print {<img src="/icons/abook.gif" border=0><a href="};
			Print lowest;ListTag(e);{?OpenDatabase">};e;{</a><br>}
		End ForAll
 ' Done with this path, get it out of the list
		Erase pathmap(lowest)
	Loop
	
	Print "</ul>"
End Sub



